D:\a\scloud-dns\scloud-dns\src\threads\windows\priority.rs
Line | Count | Source |
1 | | pub(crate) mod imp { |
2 | | use crate::threads::{ClassPriority, PriorityScope, ThreadPriority}; |
3 | | use std::io; |
4 | | |
5 | | use winapi::shared::minwindef::FALSE; |
6 | | use winapi::um::processthreadsapi::{ |
7 | | GetCurrentProcess, GetCurrentThread, SetPriorityClass, SetThreadPriority, |
8 | | }; |
9 | | use winapi::um::winbase::{ |
10 | | ABOVE_NORMAL_PRIORITY_CLASS, BELOW_NORMAL_PRIORITY_CLASS, HIGH_PRIORITY_CLASS, |
11 | | IDLE_PRIORITY_CLASS, NORMAL_PRIORITY_CLASS, REALTIME_PRIORITY_CLASS, |
12 | | THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_BELOW_NORMAL, THREAD_PRIORITY_HIGHEST, |
13 | | THREAD_PRIORITY_IDLE, THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_NORMAL, |
14 | | THREAD_PRIORITY_TIME_CRITICAL, |
15 | | }; |
16 | | |
17 | 0 | pub(crate) fn set_priority(scope: PriorityScope, p: ThreadPriority) -> io::Result<()> { |
18 | 0 | match scope { |
19 | 0 | PriorityScope::THREAD => set_thread(p), |
20 | 0 | PriorityScope::PROCESS => set_process(p), |
21 | 0 | PriorityScope::USER | PriorityScope::PROCESS_GROUP => Err(io::Error::new( |
22 | 0 | io::ErrorKind::Unsupported, |
23 | 0 | "Priority scope not supported on Windows", |
24 | 0 | )), |
25 | | } |
26 | 0 | } |
27 | | |
28 | 0 | pub(crate) fn set_class_priority(p: ClassPriority) -> io::Result<()> { |
29 | 0 | let class = match p { |
30 | 0 | ClassPriority::IDLE => IDLE_PRIORITY_CLASS, |
31 | 0 | ClassPriority::BELOW_NORMAL => BELOW_NORMAL_PRIORITY_CLASS, |
32 | 0 | ClassPriority::NORMAL => NORMAL_PRIORITY_CLASS, |
33 | 0 | ClassPriority::ABOVE_NORMAL => ABOVE_NORMAL_PRIORITY_CLASS, |
34 | 0 | ClassPriority::HIGH => HIGH_PRIORITY_CLASS, |
35 | 0 | ClassPriority::REALTIME => REALTIME_PRIORITY_CLASS, |
36 | | }; |
37 | | |
38 | 0 | let ok = unsafe { SetPriorityClass(GetCurrentProcess(), class) }; |
39 | 0 | if ok == FALSE { |
40 | 0 | Err(io::Error::last_os_error()) |
41 | | } else { |
42 | 0 | Ok(()) |
43 | | } |
44 | 0 | } |
45 | | |
46 | 0 | fn set_thread(p: ThreadPriority) -> io::Result<()> { |
47 | 0 | let prio = match p { |
48 | 0 | ThreadPriority::IDLE => THREAD_PRIORITY_IDLE, |
49 | 0 | ThreadPriority::LOW => THREAD_PRIORITY_LOWEST, |
50 | 0 | ThreadPriority::BELOW_NORMAL => THREAD_PRIORITY_BELOW_NORMAL, |
51 | 0 | ThreadPriority::NORMAL => THREAD_PRIORITY_NORMAL, |
52 | 0 | ThreadPriority::ABOVE_NORMAL => THREAD_PRIORITY_ABOVE_NORMAL, |
53 | 0 | ThreadPriority::HIGH => THREAD_PRIORITY_HIGHEST, |
54 | 0 | ThreadPriority::REALTIME => THREAD_PRIORITY_TIME_CRITICAL, |
55 | | }; |
56 | | |
57 | 0 | let ok = unsafe { SetThreadPriority(GetCurrentThread(), prio.try_into().unwrap()) }; |
58 | 0 | if ok == FALSE { |
59 | 0 | Err(io::Error::last_os_error()) |
60 | | } else { |
61 | 0 | Ok(()) |
62 | | } |
63 | 0 | } |
64 | | |
65 | 0 | fn set_process(p: ThreadPriority) -> io::Result<()> { |
66 | 0 | let class = match p { |
67 | 0 | ThreadPriority::IDLE => IDLE_PRIORITY_CLASS, |
68 | 0 | ThreadPriority::LOW => BELOW_NORMAL_PRIORITY_CLASS, |
69 | 0 | ThreadPriority::BELOW_NORMAL => BELOW_NORMAL_PRIORITY_CLASS, |
70 | 0 | ThreadPriority::NORMAL => NORMAL_PRIORITY_CLASS, |
71 | 0 | ThreadPriority::ABOVE_NORMAL => ABOVE_NORMAL_PRIORITY_CLASS, |
72 | 0 | ThreadPriority::HIGH => HIGH_PRIORITY_CLASS, |
73 | 0 | ThreadPriority::REALTIME => REALTIME_PRIORITY_CLASS, |
74 | | }; |
75 | | |
76 | 0 | let ok = unsafe { SetPriorityClass(GetCurrentProcess(), class) }; |
77 | 0 | if ok == FALSE { |
78 | 0 | Err(io::Error::last_os_error()) |
79 | | } else { |
80 | 0 | Ok(()) |
81 | | } |
82 | 0 | } |
83 | | } |